<?php

namespace app\admin\controller;

use think\Controller;
use app\common\model\Links as L;
use think\Model;

class Links extends Controller
{

    /**
     * 友情链接列表页
     * @return type
     */
    public function index()
    {
        return $this->fetch();
    }

    /*
     * 列表页获取josn数据
     * 
     */

    public function get_josn()
    {
        $l     = new L();
        $rs    = $l->getAll();
        $lists = ['code' => 0, 'msg' => '', 'data' => []];
        foreach ($rs as $k => $v)
        {
            $v['status']     = public_status($v['status']);
            $lists['data'][] = $v;
        }
        return $lists;
    }

    /*
     * 友情链接添加页
     * 
     */

    public function add()
    {
        $l  = new L();
        $rs = $l->getAll();
        $this->assign('rs', $rs);
        return $this->fetch();
    }

    /*
     * 友情链接修改页
     * 
     */

    public function edit()
    {
        $l = new L();
        $s = $l->getById(input('id'));
        $this->assign('s', $s);
        return $this->fetch();
    }

    /*
     * 添加修改数据
     * 
     */

    public function links_submit()
    {
        $l = new L();
        if (input('id', 0) == 0)
        {//添加
            $opt = [
                'name'   => input('name'),
                'url'    => input('url'),
                'pic'    => input('pic'),
                'note'   => input('note'),
                'sort'   => input('sort'),
                'status' => input('status'),
                'date'   => input('date'),
                'time'   => input('date'),
            ];
            $rs  = $l->add($opt);
            return js_return($rs[0], $rs[1]);
        }
        else
        {//修改
            $opt = [
                'name'   => input('name'),
                'url'    => input('url'),
                'note'   => input('note'),
                'pic'    => input('pic'),
                'sort'   => input('sort'),
                'status' => input('status'),
                'date'   => input('date'),
                'time'   => input('date'),
            ];
            $rs  = $l->modify($opt, input('id'));
            return js_return($rs[0], $rs[1]);
        }
    }

    /*
     * 删除
     * 
     */

    public function del_submit()
    {
        $l      = new L();
        $id     = input('id');
        $result = $l->destroy($id);
        if (false !== $result)
        {
            return js_return(1, "删除成功！");
        }
        else
        {
            return [0, $this->getError()];
        }
    }

}
